﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.BaseControl.Selection;
using jSLCore.Utility.Extensions;

namespace jSLApp.Client.Forms
{
    public partial class FileList : SelectControl
    {
        public FileList()
        {
            InitializeComponent();
        }

        void Search()
        {
            DoWork("Поиск файлов",(sender, args) =>
                                      {
                                          args.Result = Communication.Proxy.GetFreeFiles();
                                      },(o, eventArgs) =>
                                                           {
                                                               Files =
                                                                   ((IEnumerable<FileEntry>) eventArgs.Result).
                                                                       ToObservableCollection();
                                                           });
        }

        public ObservableCollection<FileEntry> Files
        {
            get { return (ObservableCollection<FileEntry>) grid.ItemsSource; }
            set { grid.ItemsSource = value; }
        }

        #region dialog

        public override ISelectableGrid GetSelectableGrid()
        {
            return grid;
        }

        public override bool IsOkVisibleWhenNotSelectable
        {
            get { return false; }
        }

        public override object CancelContentWhenNotSelectable
        {
            get { return "Закрыть"; }
        }

        public override bool IsStandartOkCancelEnabledWhenNotSelectable
        {
            get { return true; }
        }

        public override void LoadInitialData()
        {
            Search();
        }

        public override Control GetControlForInitialFocus()
        {
            return refreshButton;
        }

        #endregion

        private void refresh_Clicked(object sender, RoutedEventArgs e)
        {
            Search();
        }

        private void upload_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new FileUpload();
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   var fileName = (string)data;
                                   Files.Insert(0,new FileEntry(){JustLoaded = true,Name = fileName});
                               });
        }

        private void delete_Clicked(object sender, RowSelectedEventArgs e)
        {
            DoWork("Удаление файла на сервере",(o, args) => Communication.Proxy.DeleteFile(args.String),(sender1, eventArgs) =>
                                                                    {
                                                                        if(eventArgs.Error==null)
                                                                        {
                                                                            Files.Remove((FileEntry) e.RowObject);
                                                                        }
                                                                    },((FileEntry)e.RowObject).Name);
        }

        private void showFile_Clicked(object sender, RowSelectedEventArgs e)
        {
            var dlg = new PlayFile(((FileEntry)e.RowObject).Name);
            dlg.ShowDialog();
        }
    }
}
